/**
 * RangePicker日期范围组件 配合form组件使用
 * @Author: linzy
 * @Date: 2022-05-06
 * @function RangePicker
 * @param {number} presets - 日期预设 如：["昨天", "本月", "今天", "上个月"]
 *  昨 今 本 前 上 开头 <br />
 *  日 天 星期 周 月 年 结尾 <br />
 *  中间的数字可以是用中文也可以用数字 比如 今天 昨天 本周 上周 前三天 前一周 前三个月 前6个月等 <br />
 *  支持别名功能，presets传参可以使用对象类型，比如presets: [{'前三月':'三个月内'}] 属性为原名 属性值为别名
 * @param {*} ... - antd Date、RangePicker组件属性
 * @example
 *  <RangePicker />
 */
import React from "react";
import moment from "moment";
import { DatePicker } from "antd";
import { chinese2num, setDefaultAttrs } from "@/hooks/tools";
import { typeOf } from "@/hooks/tools";
const { RangePicker } = DatePicker;

/**
 * 获取日期预设
 * @ignore
 * @param {*} arr
 * @returns
 */
function getPresets(arr = []) {
  if (arr.length === 0) return {};
  var tempObj = {};
  arr.forEach((ele) => {
    let item = ele,
      alias = ele;
    if (typeOf(ele) === "object") {
      const [[key, val]] = Object.entries(ele);
      item = key;
      alias = val;
    }
    let preAmount, curAmount, unit, method;
    if (/天|日$/.test(item)) {
      unit = "day";
    } else if (/周|星期$/.test(item)) {
      unit = "week";
    } else if (/月$/.test(item)) {
      unit = "month";
    } else if (/年$/.test(item)) {
      unit = "year";
    } else {
      throw new Error("日期预设，请输入预设单位，如日、月、年等");
    }
    if (/^(今|本)/.test(item)) {
      method = "subtract";
      preAmount = 0;
      curAmount = 0;
    } else if (/^(昨|上|去)/.test(item)) {
      method = "subtract";
      preAmount = 1;
      curAmount = 1;
    } else if (/^(明|下)/.test(item)) {
      method = "add";
      preAmount = 1;
      curAmount = 1;
    } else if (/^前/.test(item)) {
      method = "subtract";
      let num = chinese2num(item);
      if (/\d+/.test(item)) {
        preAmount = /\d+/.exec(item)[0] - 1;
      } else if (num) {
        preAmount = num;
      }
      curAmount = 1;
    } else if (/^近/.test(item)) {
      method = "subtract";
      let num = chinese2num(item);
      if (/\d+/.test(item)) {
        preAmount = /\d+/.exec(item)[0] - 1;
      } else if (num) {
        preAmount = num - 1;
      }
      curAmount = 0;
    } else if (/^后/.test(item)) {
      method = "add";
      let num = chinese2num(item);
      if (/\d+/.test(item)) {
        curAmount = +/\d+/.exec(item)[0] + 1;
      } else if (num) {
        curAmount = num + 1;
      }
      preAmount = 1;
    } else {
      throw new Error(
        "日期预设，请输入具体日期 比如 今天 昨天 本周 上周 下周 前三天 前一周 前三个月 后三个月 前6个月等"
      );
    }
    try {
      tempObj[alias] = [
        moment()[method](preAmount, unit).startOf(unit),
        moment()[method](curAmount, unit).endOf(unit),
      ];
    } catch (error) {
      console.error("🚀 ==> error", error);
    }
  });
  return tempObj;
}
export default (props) => {
  const { presets, mode, ...restProps } = props;
  let defaultArr = [];
  if (typeof mode === "string") {
    restProps.mode = [mode, mode];
  } else {
    restProps.mode = ["datetime", "datetime"];
  }
  const type = restProps.mode[0];
  let format = "YYYY-MM-DD HH:mm:ss";
  let showTime = false;
  switch (type) {
    case "datetime":
      defaultArr = ["昨天", "本月", "今天", "上个月"];
      showTime = {
        format: "HH:mm:ss",
        defaultValue: [
          moment("00:00:00", "HH:mm:ss"),
          moment("23:59:59", "HH:mm:ss"),
        ],
      };
      break;
    case "dateMinute":
      defaultArr = ["昨天", "本月", "今天", "上个月"];
      format = "YYYY-MM-DD HH:mm";
      showTime = {
        format: "HH:mm",
        defaultValue: [
          moment("00:00", "HH:mm"),
          moment("23:59", "HH:mm"),
        ],
      };
      break;
    case "date":
      defaultArr = ["昨天", "本月", "今天", "上个月"];
      format = "YYYY-MM-DD";
      break;
    case "week":
      defaultArr = ["本周", "上周", "前三周", "前六周"];
      format = "YYYY-Wo";
      break;
    case "month":
      defaultArr = ["本月", "上个月", "前三个月", "前六个月"];
      format = "YYYY-MM";
      break;
    case "year":
      defaultArr = ["今年", "去年", "前三年"];
      format = "YYYY";
      break;
    default:
      break;
  }
  setDefaultAttrs(restProps, {
    style: {
      width: "100%",
    },
    format,
    showTime,
    ranges: getPresets(presets || defaultArr),
  });
  return <RangePicker {...restProps} />;
};
